package Gui;

import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Vector;

import javax.mail.search.SearchTerm;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;

import pdf.PDFTrans;

import Logic.LoginInfo;
import client.LibaryMain;

import java.awt.Panel;
import java.awt.ScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.awt.Desktop;
/**
 * this class impliments the search results gui 
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class SearchResult extends JPanel {
	private JTextField textField;
	private JTextField textField_1;
	private JTextField text;  
	public static String copypdfid;
	public Object o;
	public SearchResult(String [] str) {
		setBackground(new Color(153, 204, 255));
		init(str);
 
	}
	public void init (final String [] str) {
		 	
			Vector<Vector<Object>> data = new Vector<Vector<Object>>();	// A vector that contain all the table data- each cell is a vector itself
			
	        for (int i=2; i<=(str.length-4);i=i+4){		// 
	        Vector<Object> row = createrow(str,i);
	        data.add(row);
	        }
	        Vector<String> headers = new Vector<String>();	//Building a columns names vector 
	        System.out.println(">>>>>>>>>>>>>>>>>>>>>>"+data);
	        if (str[1].equals("nofreecopy"))	//check if the result contain free copies or lend ones. 
	        {
	        	headers.add("Copy ID");
	        	headers.add("Title");
	        	headers.add("Author");
	        	headers.add( "Nearest End Date");
	        }
	        else
	        {
	        	headers.add("Copy ID");
	        	headers.add("Title");
	        	headers.add("Author");
	        	headers.add( "Place On Shelf");
	        	
	        }
	        
	        final JTable table = new JTable( data, headers)
	        {
	               public boolean isCellEditable(int data, int columns)
	               {
	                   if(columns>=1)
	            	     return false;
	                   else return true;
	               }
	        };
	       
	        /*table.addMouseListener(new MouseAdapter()    
	        {   
	        public void mouseClicked(MouseEvent evt)   
	        {   
	        int col = table.getSelectedColumn();   
	        int row = table.getSelectedRow();
	        System.out.println(""+col+row);
	        Object o=((String)table.getValueAt(row, col));
	        System.out.println(""+o);
	        }   
	        }); */
	        final JButton btnOrderBook = new JButton("Order Book");
	        btnOrderBook.setFont(new Font("Tahoma", Font.BOLD, 13));
	        btnOrderBook.setEnabled(false);
	        table.setFillsViewportHeight(true);
	        table.setRowHeight(20);
	        table.setPreferredScrollableViewportSize(new Dimension(900,150));
	        JScrollPane scrollPane = new JScrollPane(table);
	        table.addMouseListener(new MouseAdapter()    
	        {   
	        public void mouseClicked(MouseEvent evt)   
	        {  
	        final String user;
	        int col = table.getSelectedColumn();   
	        int row = table.getSelectedRow();
	        boolean is=table.isColumnSelected(0);
    		
			if (!is){
			
				
				System.out.println("wrong selection");
				JOptionPane.showMessageDialog(null, "Please Select only Book ID");

			}
			else{
				    o=((String)table.getValueAt(row, col));
					Object o2=((String)table.getValueAt(row, col+1));
					copypdfid=(String)o2;
					
			}
	        System.out.println(""+col+row);
	       
	        
	       
	    
	        user=LoginInfo.UserName;
	        System.out.println("--------------------------------"+user);
	        if (str[1].equals("nofreecopy") & DirectorWindow.roll.equals("SubscriberWindow"))
	        {
	        	btnOrderBook.setEnabled(true);
	        }
	        btnOrderBook.addActionListener(new java.awt.event.ActionListener() {     
	            public void actionPerformed(java.awt.event.ActionEvent ev) {
	            	btnOrderBook(ev);
	            					
				}

				private void btnOrderBook(ActionEvent ev) {
					ArrayList<String> OrederArr=new ArrayList<String>();
					OrederArr.add("OrderBook");
					OrederArr.add(user);
					OrederArr.add((String) o);
					LibaryMain.chat.handleMessageFromGui(OrederArr);
					
					
				}
	        });
	        
	        }   
	        });



	        
	        JButton btnLogout = new JButton("Logout");
	        btnLogout.setFont(new Font("Tahoma", Font.BOLD, 13));
			btnLogout.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					ArrayList<String> Arr=new ArrayList<String>();
					Arr.add("Logout");
					Arr.add("Subscriber");
					Arr.add(LoginInfo.UserName);
					LibaryMain.chat.handleMessageFromGui(Arr);
					MainScreen.cardLayot.show(MainScreen.card, "Login");
					
				}
			});
	        
	        JButton btnBack = new JButton("Back");
	        btnBack.setFont(new Font("Tahoma", Font.BOLD, 13));
			btnBack.addActionListener(new java.awt.event.ActionListener() {     //  back to  subscriber 
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	            	btnBackActionPerformed(evt);
	            
	            }

						private void btnBackActionPerformed(ActionEvent evt) {
							
							MainScreen.cardLayot.show(MainScreen.card, "Search");
							
						}
					
	        });
	        
	        JButton btnTest = new JButton("Open PDF");
	        btnTest.addActionListener(new ActionListener() {
	        	public void actionPerformed(ActionEvent arg0) {
	        		(new Thread(new PDFTrans())).start();
	        		ArrayList<String> Arr=new ArrayList<String>();
					Arr.add("GiveBook");
					Arr.add(copypdfid+".pdf");
					LibaryMain.chat.handleMessageFromGui(Arr);
					
					String yyy = copypdfid+".pdf";
					File file = new File("C:/"+yyy);
			        if (file.toString().endsWith(".pdf"))
						
			        	try {
							Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
			       
					
			    	try {
						Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			    	
				
			    }
					
			
	        	
	        });
	        
	
	        GroupLayout groupLayout = new GroupLayout(this);
	        groupLayout.setHorizontalGroup(
	        	groupLayout.createParallelGroup(Alignment.LEADING)
	        		.addGroup(groupLayout.createSequentialGroup()
	        			.addContainerGap()
	        			.addComponent(btnBack, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
	        			.addPreferredGap(ComponentPlacement.RELATED, 663, Short.MAX_VALUE)
	        			.addComponent(btnLogout, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
	        			.addGap(44))
	        		.addGroup(groupLayout.createSequentialGroup()
	        			.addGap(285)
	        			.addComponent(btnOrderBook, GroupLayout.PREFERRED_SIZE, 239, GroupLayout.PREFERRED_SIZE)
	        			.addContainerGap(380, Short.MAX_VALUE))
	        		.addGroup(groupLayout.createSequentialGroup()
	        			.addGap(54)
	        			.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 801, GroupLayout.PREFERRED_SIZE)
	        			.addContainerGap(34, Short.MAX_VALUE))
	        		.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
	        			.addContainerGap(507, Short.MAX_VALUE)
	        			.addComponent(btnTest)
	        			.addGap(306))
	        );
	        groupLayout.setVerticalGroup(
	        	groupLayout.createParallelGroup(Alignment.LEADING)
	        		.addGroup(groupLayout.createSequentialGroup()

	        			.addGap(5)
	        			.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	        			.addPreferredGap(ComponentPlacement.RELATED)
	        			.addComponent(btnOrderBook)
	        			.addGap(80)
	        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
	        				.addComponent(btnBack)
	        				.addComponent(btnLogout))
	        			.addGap(69)
	        			.addComponent(btnTest)
	        			.addGap(131))

	        			.addGap(21)
	        			.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 394, GroupLayout.PREFERRED_SIZE)
	        			.addGap(64)
	        			.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
	        				.addGroup(groupLayout.createSequentialGroup()
	        					.addComponent(btnOrderBook, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
	        					.addGap(100)
	        					.addComponent(btnBack)
	        					.addGap(71))
	        				.addGroup(groupLayout.createSequentialGroup()
	        					.addComponent(btnLogout)
	        					.addGap(62))));

	     
	        setLayout(groupLayout);
	        

		    }
	
	
	private Vector<Object> createrow(String[] str, int i) {		//A function that build a row for table
		 Vector<Object> row = new Vector<Object>();
        row.add( ""+str[i]);
        row.add( ""+str[i+1]);
        row.add( ""+str[i+2]);
        row.add( ""+str[i+3]);
        
       
		return row;
	}
}

/*        File file = new File("C:/Users/user/Desktop/1/oBL/src/Gui/PDF/war and peace.pdf");
        if (file.toString().endsWith(".pdf"))
			try {
				Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		else {
            Desktop desktop = Desktop.getDesktop();
            try {
				desktop.open(file);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    }*/
